package cn.ctodb.divination.service;

import cn.ctodb.divination.domain.Hzwx;
import cn.ctodb.divination.repository.HzwxRepository;
import cn.ctodb.divination.repository.search.HzwxSearchRepository;
import cn.ctodb.divination.service.dto.HzwxDTO;
import cn.ctodb.divination.service.mapper.HzwxMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing Hzwx.
 */
@Service
@Transactional
public class HzwxService {

    private final Logger log = LoggerFactory.getLogger(HzwxService.class);

    private final HzwxRepository hzwxRepository;

    private final HzwxMapper hzwxMapper;

    private final HzwxSearchRepository hzwxSearchRepository;

    @Inject
    private BihuaService bihuaService;

    public HzwxService(HzwxRepository hzwxRepository, HzwxMapper hzwxMapper, HzwxSearchRepository hzwxSearchRepository) {
        this.hzwxRepository = hzwxRepository;
        this.hzwxMapper = hzwxMapper;
        this.hzwxSearchRepository = hzwxSearchRepository;
    }

    /**
     * Save a hzwx.
     *
     * @param hzwxDTO the entity to save
     * @return the persisted entity
     */
    public HzwxDTO save(HzwxDTO hzwxDTO) {
        log.debug("Request to save Hzwx : {}", hzwxDTO);
        Hzwx hzwx = hzwxMapper.hzwxDTOToHzwx(hzwxDTO);
        hzwx = hzwxRepository.save(hzwx);
        HzwxDTO result = hzwxMapper.hzwxToHzwxDTO(hzwx);
        hzwxSearchRepository.save(hzwx);
        return result;
    }

    /**
     * Get all the hzwxes.
     *
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public List<HzwxDTO> findAll() {
        log.debug("Request to get all Hzwxes");
        List<HzwxDTO> result = hzwxRepository.findAll().stream()
            .map(hzwxMapper::hzwxToHzwxDTO)
            .collect(Collectors.toCollection(LinkedList::new));

        return result;
    }

    /**
     * Get one hzwx by id.
     *
     * @param id the id of the entity
     * @return the entity
     */
    @Transactional(readOnly = true)
    public HzwxDTO findOne(Long id) {
        log.debug("Request to get Hzwx : {}", id);
        Hzwx hzwx = hzwxRepository.findOne(id);
        HzwxDTO hzwxDTO = hzwxMapper.hzwxToHzwxDTO(hzwx);
        return hzwxDTO;
    }

    /**
     * Delete the  hzwx by id.
     *
     * @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete Hzwx : {}", id);
        hzwxRepository.delete(id);
        hzwxSearchRepository.delete(id);
    }

    /**
     * Search for the hzwx corresponding to the query.
     *
     * @param query the query of the search
     * @return the list of entities
     */
    @Transactional(readOnly = true)
    public List<HzwxDTO> search(String query) {
        log.debug("Request to search Hzwxes for query {}", query);
        return StreamSupport
            .stream(hzwxSearchRepository.search(queryStringQuery(query)).spliterator(), false)
            .map(hzwxMapper::hzwxToHzwxDTO)
            .collect(Collectors.toList());
    }

    public String getHzwx(char c) {
        Hzwx hzwx = hzwxRepository.findByHzLike("%" + c + "%");
        return hzwx.getWh();
    }

    public List<Map<String, String>> zywx(String xing, String ming) {
        List<Map<String, String>> list = new ArrayList<>();
        char x1 = xing.charAt(0);
        list.add(zywx(x1));
        if (xing.length() > 1) {
            x1 = xing.charAt(1);
            list.add(zywx(x1));
        }
        x1 = ming.charAt(0);
        list.add(zywx(x1));
        if (ming.length() > 1) {
            x1 = ming.charAt(1);
            list.add(zywx(x1));
        }
        return list;
    }

    private Map<String, String> zywx(char c) {
        Map<String, String> m = new HashMap<>();
        m.put("hz", c + "");
        m.put("bihua", bihuaService.getNum(c) + "");
        m.put("wuxing", getHzwx(c));
        return m;
    }

}
